package io.hx.modules.patient.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.hx.common.validator.ValidatorUtils;
import io.hx.modules.patient.entity.PatientUserEntity;
import io.hx.modules.patient.service.PatientUserService;
import io.hx.modules.rep.entity.DiagnosticEntity;
import io.hx.modules.rep.service.DiagnosticService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.hx.modules.patient.entity.FilesEntity;
import io.hx.modules.patient.service.FilesService;
import io.hx.common.utils.PageUtils;
import io.hx.common.utils.R;



/**
 * 患者档案
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2023-03-22 14:51:39
 */
@RestController
@RequestMapping("patient/files")
public class FilesController {
    @Autowired
    private FilesService filesService;
    @Autowired
    private DiagnosticService diagnosticService;
    @Autowired
    private PatientUserService patientUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("patient:files:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = filesService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{visitId}")
    @RequiresPermissions("patient:files:info")
    public R info(@PathVariable("visitId") String visitId){
        FilesEntity files = filesService.getById(visitId);
        //诊断报告记录
        List<DiagnosticEntity> diagnosticEntityList =diagnosticService.queryByUserId(files.getUserId());
        //用户信息
        PatientUserEntity patientUserEntity = patientUserService.queryById(files.getUserId());

        System.out.println("用户信息:"+patientUserEntity.toString());
        System.out.println("长度:"+diagnosticEntityList.size());

        files.setDiagnosticEntityList(diagnosticEntityList);
        files.setUser(patientUserEntity);
        System.out.println(files.toString());
        return R.ok().put("files", files);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("patient:files:save")
    public R save(@RequestBody FilesEntity files){
        filesService.save(files);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("patient:files:update")
    public R update(@RequestBody FilesEntity files){
        ValidatorUtils.validateEntity(files);
        filesService.updateById(files);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("patient:files:delete")
    public R delete(@RequestBody String[] visitIds){
        filesService.removeByIds(Arrays.asList(visitIds));

        return R.ok();
    }

}
